<?php
namespace app\api\controller;
use app\common\controller\BaseApi;
/**
 * 上传
 */
class UploadFace extends  BaseApi{
	public function _initialize(){
	}
	/**
	 * 普通上传
	 * http://smartgymdemo.ocmcom.com/api/UploadFace/upload_img
	 */
	public function upload_img(){
	 	 $file = request()->file('file');
		 $shop_id=input("shop_id",0);
		 $card_no=input("card_no","");
	 	 if(!empty($file)){
	 	 	 $info = $file->move(ROOT_PATH . 'data' . DS . 'face'.DS.$shop_id.DS.date("Ymd"),"");
			 $headimage_url="/data/face/$shop_id/".date("Ymd")."/".$info->getFilename();
			 if(!empty($shop_id)&&!empty($card_no)){
			 	$this->save_extral($card_no,$shop_id,$headimage_url);
			 }
			 return $this->ajax_success($headimage_url);
	 	 }else{
	 	 	return $this->ajax_error("文件为空");
	 	}
	}
	//重机 向主机上传图片
	public function upload(){
		if(!empty(input("files",''))){
			$dir="./data/face/".date("Ymd")."/";
			if(!file_exists($dir)){
				mkdir($dir);
			}
			$file_name=$dir.input("filename");
			$fopen=fopen($file_name, "wb");			
			file_put_contents($file_name,base64_decode(str_replace(" ", "+",input("files"))));
			fclose($fopen);		
			return $this->ajax_success();		
		}else{
			return $this->ajax_error("文件为空");
		}
	} 
	//测试图片上传
	 public function test_upload(){
	  	$client = new \GuzzleHttp\Client();
		$response=$client->request('POST',"http://SmartGYM.com/api/UploadFace/UploadDoorConfig", [
			'multipart' => [
	        [
	            'name'     => 'files[]',
	            'contents' => fopen( iconv ( 'UTF-8', 'GBK', "./data/face/thumb-1.jpg" ), 'r')
	        ]
	        
		]]);
		echo $response->getBody();
	}
//	 /**
//	  * 获取闸机配置文件
//	  * http://smartgym.com/api/UploadFace/GetDoorConfig
//	  */
	 public function GetDoorConfig(){
		 $shop_id=input("shop_id");
		 if(empty($shop_id)){
		 	return $this->ajax_error("店铺ip 为空");
		 }
		 $door_url="./data/doors/$shop_id/";
		 if(!is_dir($door_url)){
		 	return $this->ajax_error("","","文件夹不存在");
		 }
		 $result="";
		 $files=scandir($door_url);
		 foreach ($files as $file) {
		 	if(strlen($file)>4){
		 		$result[]="http://".$_SERVER['HTTP_HOST'].$door_url.iconv("gb2312","utf-8",$file);	
		 	}
		} 
		if(empty($result)){
			return $this->ajax_error("","","文件不存在");
		}
		return $this->ajax_success(implode(",", $result));
	 }
	 /**
	  * 上传闸机配置文件路径
	  * http://smartgym.com/api/UploadFace/UploadDoorConfig
	  */
	 public function UploadDoorConfig(){
	 	 $files= request()->file('files');
		 if(count($files)<1){
		 	return $this->ajax_error("","","文件不存在");
		 }
		 $shop_id=input("shop_id",0);
		 foreach ($files as $file) {
	 	 	if(!empty($file)){
		 	 	 $info = $file->move(ROOT_PATH . 'data' . DS . 'doors'.DS.$shop_id,"");
			} 
	 	}
		 return $this->ajax_success("","","成功");
	}
	 /**
	  * 保存扩展
	  */
	  function save_extral($card_no,$shop_id,$headimage_url,$has_return=0){
	  	$extral=db("InterfaceExtral")->where("card_no='$card_no' and shop_id=$shop_id")->find();
		 if(empty($extral)){
		 	db("InterfaceExtral")->insert(array("card_no"=>$card_no,"shop_id"=>$shop_id,"head_image"=>$headimage_url));
		 }else{
		 	db("InterfaceExtral")->where("card_no='$card_no' and shop_id=$shop_id")->update(array("head_image"=>$headimage_url));
		 }
		 if($has_return!=0){
		 	 return $this->ajax_success();
		 }
	  }
	  
	/*
	 * 人脸比对
	 * http://smartgym.com/api/UploadFace/faceapp
	 * */
	public function faceapp(){
		$url="https://api-cn.faceplusplus.com/facepp/v3/compare";
		$post=array(
			"api_key"=> "vBGv32M0fB1RXrBo4Zgog7hCuKRS_1Lm",
			"api_secret"=> "b7pvYxjsv_TRbw3FyH7pgF1wmUDSMGGq",
			"image_url1"=> input("url1","https://zwzlcloud.ocmcom.com/data/upload/20180119/wx40605d18d836bc2d.o6zAJs11V5PTOxRiVTBhvW8MT9yo.51c4461409e5d3b84f8a15f364958df2.jpg"),
			"image_url2"=>input("url2","https://zwzlcloud.ocmcom.com/data/upload/20180119/wx40605d18d836bc2d.o6zAJs11V5PTOxRiVTBhvW8MT9yo.51c4461409e5d3b84f8a15f364958df2.jpg"),
		);
		return $this->ajax_success("","",$this->curl_post($url, $post));
	}   
	
	function curl_post($url, $post){
		$options = array(
		CURLOPT_RETURNTRANSFER =>true,
		CURLOPT_HEADER =>false,
		CURLOPT_POST =>true,
		CURLOPT_POSTFIELDS => $post,
		CURLOPT_SSL_VERIFYPEER=>FALSE,
		CURLOPT_SSL_VERIFYHOST=>FALSE,
		);
		$ch = curl_init($url);
		curl_setopt_array($ch, $options);
		$result = curl_exec($ch);
		curl_close($ch);
		return $result;
	}	
}
